<template>
  <NEl class="p-4 top-box"
    ><div class="fs-2">留言帖</div>
    <div>
      <NInput>
        <NInput v-model:value="queryInfo.title" placeholder="标题搜索"></NInput>
        <NButton type="primary" @click="search">查询</NButton>
      </NInput>
    </div>
    <div>
      <NButton @click="showAdd">发布帖子</NButton>
    </div>
    <div></div>
    <HuUserInfo></HuUserInfo>
  </NEl>

  <div class="pl-20 pr-20" style="width: 50vw; margin: 0px auto">
    <!-- 切换排序 -->
    <NTabs @update-value="changeOrder">
      <NTabPane name="1" tab="最新"></NTabPane>
      <NTabPane name="2" tab="高赞"></NTabPane>
      <NTabPane name="3" tab="热门"></NTabPane>
      <NTabPane name="4" tab="热评"></NTabPane>
    </NTabs>

    <!-- 骨架屏效果 -->
    <div v-if="loading && firstLoad">
      <div v-for="i in 10" :key="i" class="ml-20 mr-20 mb-2">
        <NCard>
          <NSkeleton text></NSkeleton>
          <NSkeleton text></NSkeleton>
          <div class="img-box">
            <NSkeleton class="vimg" text></NSkeleton>
            <NSkeleton class="vimg" text></NSkeleton>
            <NSkeleton class="vimg" text></NSkeleton>
          </div>
        </NCard>
      </div>
    </div>
    <!-- 数据列表 -->
    <div v-else>
      <div v-for="d in list" :key="d.mid" class="mb-3">
        <!-- 无图模式 -->
        <div>
          <NCard :title="d.user.info.nickname">
            <div>
              <div class="fs-3" @click="toDetail(d.mid)">{{ d.title }}</div>
              <div class="detail mb-2 mt-2">
                <div>
                  <span @click="toUser(d.uid)">{{
                    d.user.info.nickname || d.user.user.username
                  }}</span>
                  -
                  {{ Tools.formatDate(d.lastupdate) }}点击量：{{ d.hits }}
                </div>

                <div>
                  <NPopover trigger="hover">
                    <span>点赞量:{{ d.praise }}</span>
                    <template #trigger>
                      <div @click="likeNews(d)" :class="{ active: d.exInfo.praised }">赞</div>
                    </template>
                  </NPopover>
                </div>
              </div>
            </div>
          </NCard>
        </div>
      </div>
    </div>
  </div>
  <div class="p-4 tc" v-if="loading">
    <span> <NSpin size="small"></NSpin> 正在加载中...</span>
  </div>
  <div class="p-4 tc" v-if="page.pageNumber >= page.pageCount && !loading">我是有底线的</div>

  <NBackTop></NBackTop>

  <!-- 发布帖子的对话框 -->
  <div>
    <NModal preset="dialog" v-model:show="visible" title="发布帖子">
      <div>
        <NInput type="textarea" v-model:value="content.title" placeholder="标题"></NInput>
        <NInput type="textarea" v-model:value="content.info" placeholder="帖子内容"></NInput>
      </div>

      <template #action>
        <NButton type="primary" @click="addReply">发布</NButton>
      </template>
    </NModal>
  </div>
</template>

<script setup lang="ts">
import { Api } from '@/types/Api'
import { Tools } from '@/types/Tools'
import type { BaseDataResult, BaseResult, PageBean, PageVO } from '@/types/BaseResult'
import type { TbMessage } from '@/types/message'
import { ref, onMounted, onUnmounted } from 'vue'
import {
  NBackTop,
  NButton,
  NCard,
  NEl,
  NInput,
  NModal,
  NPopover,
  NSkeleton,
  NSpin,
  NTabPane,
  NTabs,
  useMessage,
} from 'naive-ui'

import router from '@/router'
import HuUserInfo from '@/components/HuUserInfo.vue'

const message = useMessage()

const loading = ref(false)
const firstLoad = ref(true)

//#region 查询的部分
const page = ref({ pageNumber: 1, pageSize: 10 } as PageBean)
const list = ref([] as TbMessage[])
const queryInfo = ref({ order: 1, title: '' })

const query = () => {
  loading.value = true
  Api.get(
    `/message/${page.value.pageSize}/${page.value.pageNumber}/1`,
    { title: queryInfo.value.title },
    (data: BaseDataResult<PageVO<TbMessage>>) => {
      loading.value = false
      if (data.success) {
      }
      list.value = data.data.list
      page.value = data.data.pageBean
    },
  )
}

const changeOrder = (order: number) => {
  queryInfo.value.order = order
  requery()
}

const search = () => {
  firstLoad.value = true
  page.value.pageNumber = 1
  list.value = []
  query()
}

const requery = () => {
  firstLoad.value = true
  page.value.pageNumber = 1
  queryInfo.value.title = ''
  list.value = []
  query()
}
//#endregion

//#region 发布帖子
const visible = ref(false)
const content = ref({
  title: '',
  info: '',
})

const showAdd = () => {
  content.value = {
    title: '',
    info: '',
  }
  visible.value = true
}

const addReply = () => {
  loading.value = true
  Api.post(`/message`, content.value, (data: BaseResult) => {
    loading.value = false
    if (data.success) {
      visible.value = false
      message.success(data.message)
    } else {
      message.error(data.message)
    }
  })
}
//#endregion

//#region 点赞
const likeNews = (news: TbMessage) => {
  Api.post(`/message/praise/${news.mid}`, {}, (data: BaseResult) => {
    if (data.success) {
      news.exInfo.praised = !news.exInfo.praised
      return
    }
    message.error(data.message)
  })
}
//#endregion

//#region 详情
const toDetail = (mid: number) => {
  router.push(`/message/${mid}`)
}

const toUser = (uid: number) => {
  router.push(`/message/user/${uid}`)
}
//#endregion

onMounted(() => {
  query()
})
onUnmounted(() => {})
</script>

<style scoped lang="scss">
.top-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  position: sticky;
  top: 0px;
  z-index: 10;
  background-color: var(--body-color);
}

.vimg {
  display: block;
  height: 7vw;
}

.img-box {
  display: flex;
  gap: 1rem;
  > div img {
    flex: 1;
  }
  img {
    display: block;
    aspect-ratio: 16 / 9;
    width: 100%;
    object-fit: cover;
  }
}
.detail {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .active {
    color: #ff0000;
  }
}
</style>
